/**
 * Created by hiberk on 2017/12/29.
 */
/** distribution的controller 试用申请 */
tzadmin.controller('distribution', ['$scope', 'Utils', 'HttpUtils', 'Datepicker', 'SimpleUiGrid', 'ngDialog', 'toaster',
    function($scope, utils, httpUtils, datepickerService, simpleUiGridService, ngDialog, toaster) {
        var createDate = datepickerService.init($scope,{initValue: ''});

        var trustUserGridColumns = [
            {
                displayName: '公司名称',
                field: 'company_name',
                width: '150'
            },
            {
                displayName: '联系人',
                field: 'contact_name',
                width: '100'
            },
            {
                displayName: '手机号',
                field: 'mobile',
                width: '150'
            },
            {
                displayName: '邮箱',
                field: 'email'
            },
            {
                displayName: '需求描述',
                field: 'requirement_description',
                width: '80'
            },
            {
                displayName: '分配状态',
                field: 'distribute_status',
                width: '80',
                cellClass: "align-right"
            },
            {
                displayName: '提交时间',
                field: 'distribute_time',
                width: '80'
            },
            {
                name: '操作',
                field: 'operate',
                width: '150',
                enableCellEdit: false,
                cellTemplate: '\
                    <div class="ui-grid-cell-contents" style="text-align:center;">\
                        <button ng-click="grid.appScope.editTrustUser(row.entity)" title="分配" class="btn btn-xs btn-success">\
                            <em class="fa fa-edit"></em>\
                        </button>\
                '
            }
        ];

        $scope.queryParams = {namespace: 'DistributeMapper.queryDistributionList'};
        $scope.searchTrustUserGrid = function() {
            $scope.queryParams.createDate = utils.dateFormat(createDate.getDate(),'yyyy-MM-dd');
            trustUserGrid.draw($scope.queryParams);
        };

        var trustUserGrid = simpleUiGridService.init($scope,
            {
                optionModel: 'trustUserGridOptions',
                pageOptionModel: 'trustUserGridPageOptions',
                gridApi: 'trustUserGridApi',
                columns: trustUserGridColumns,
                pageType: 'common',
                needExport: false,
                remoteSort: true,
                controllerSearchFn: $scope.searchTrustUserGrid
            }
        );

        $scope.editFee = function(data){
            ngDialog.openConfirm({
                template: 'editFeeDialog',
                className: 'ngdialog-theme-default',
                appendClassName: 'ngdialog-lg',
                scope: $scope,
                controller: ['$scope', function($scope) {
                    $scope.getTrustUserFee(data.tid);
                }]
            }).then(function (value) {
                httpUtils.post('/trustUserApiFee/save', $scope.userFee,
                    function(result){
                        if (result.success){
                            toaster.pop('success', '', '保存成功');
                        }else{
                            toaster.pop('error', '', result.msg);
                        }
                    },
                    function(result){
                        toaster.pop('error', '', '保存失败');
                    }
                );
            });
        }

        $scope.getTrustUserFee = function(id){
            httpUtils.queryList(
                {
                    namespace:'AccountMapper.getTrustUserFee',
                    id: id
                },
                function(result){
                    if (result.success){
                        $scope.userFee = result.datas.gridDatas[0];
                    }else{
                        $scope.userFee = null;
                    }
                },
                function(result){
                    toaster.pop('error', '', '获取用户计费失败');
                }
            );
        }

        $scope.searchTrustUserGrid();
    }
]);